<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
    // 模拟数据库
    Map<String, String> userDatabase = (Map<String, String>)application.getAttribute("userDatabase");
    if(userDatabase == null) {
        userDatabase = new HashMap<>();
        application.setAttribute("userDatabase", userDatabase);
    }

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");

    // 简单验证
    if(username == null || username.trim().isEmpty() ||
            password == null || password.trim().isEmpty() ||
            email == null || email.trim().isEmpty()) {
        request.setAttribute("msg", "所有字段都必须填写！");
        request.getRequestDispatcher("register.jsp").forward(request, response);
        return;
    }

    // 检查用户名是否已存在
    if(userDatabase.containsKey(username)) {
        request.setAttribute("msg", "用户名已存在，请选择其他用户名！");
        request.getRequestDispatcher("register.jsp").forward(request, response);
        return;
    }

    // 模拟存储用户信息（实际项目中应该加密密码）
    userDatabase.put(username, password);

    // 可以在这里存储更多用户信息，如email等

    // 注册成功，跳转到登录页面
    request.setAttribute("msg", "注册成功，请登录！");
    request.getRequestDispatcher("login.jsp").forward(request, response);
%>